package com.zlc.study.controller;

import cn.hutool.core.date.DateUtil;
import com.zlc.study.entities.LawPrompt;
import com.zlc.study.service.LawAssistant;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.input.Prompt;
import dev.langchain4j.model.input.PromptTemplate;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@Slf4j
public class ChatPromptController {
    @Resource
    private LawAssistant lawAssistant;
    @Resource
    private ChatModel chatModel;

    @GetMapping(value = "/chatprompt/test1")
    public String test1(){
        String chat1 = lawAssistant.chat("什么是知识产权",2000);
        System.out.println(chat1);
        String chat2 = lawAssistant.chat("我是谁",2000);
        System.out.println(chat2);
        String chat3 = lawAssistant.chat("你是谁",2000);
        System.out.println(chat3);
        return "success : "+ DateUtil.now()+"<br> \n\n chat1: "+chat1+"<br> \n\n chat2: "+chat2;
    }


    @GetMapping(value = "/chatprompt/test2")
    public String test2(){
        LawPrompt lawPrompt = new LawPrompt();
        lawPrompt.setLegal("知识产权");
        lawPrompt.setQuestion("TRIPS协议");

        String chat = lawAssistant.chat(lawPrompt);
        System.out.println(chat);
        return "success : "+ DateUtil.now()+"<br> \n\n chat: "+chat;
    }

    @GetMapping(value = "/chatprompt/test3")
    public String test3(){
        String role = "外科医生";
        String question = "牙疼";

        //1.构造PromptTemplate模板
        PromptTemplate template = PromptTemplate.from("你是一个{{it}}助手，{{question}}怎么办？");

        //2.由PromptTemplate生成Prompt
        Prompt prompt = template.apply(Map.of("it", role, "question", question));

        //3.Prompt提示词编程UserMessage
        UserMessage userMessage = prompt.toUserMessage();

        //4.调用大模型
        ChatResponse chatResponse = chatModel.chat(userMessage);

        //4.1后台打印
        System.out.println(chatResponse.aiMessage().text());
        //4.2前台返回
        return "success : "+ DateUtil.now()+"<br> \n\n chat: "+chatResponse.aiMessage().text();
    }
}
